package com.apes.fn.business.insurance.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apes.fn.business.insurance.modle.InsuranceSaleTask;
import com.apes.fn.business.insurance.repository.InsuranceSaleTaskDao;
import com.apes.fn.business.insurance.repository.InsuranceSaleTaskRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.api.SpringManager;
import com.apes.framework.util.Arith;
import com.apes.framework.util.DateUtil;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.frame.repository.DeptRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

/**
 * @ClassName InsuranceSaleTaskService
 * @Author wsj
 * @Date 2021/5/21 17:14
 * @description 业务层
 */
@Service("insuranceSaleTaskService")
public class InsuranceSaleTaskService extends DomainService {
    @Autowired
    private InsuranceSaleTaskRepository taskRepository;
    @Autowired
    private InsuranceSaleTaskDao saleTaskDao;

    public void statisticInsuranceSaleTask() {
        JSONObject param = new JSONObject();
        param.put("taskDate", DateUtil.dateToDate(new Date(), DateUtil.SHOW_DATE_FORMAT));
        JSONArray saleDataArray = saleTaskDao.findInsuranceSaleData(param);
        JSONArray strategies = saleTaskDao.findBusinessStrategyList(param);
        if (saleDataArray.size() >0 && strategies.size() > 0) {
            strategies.stream().map(item -> (JSONObject) item).forEach(strategy -> {
                InsuranceSaleTask saleTask = new InsuranceSaleTask();
                Dept dept = SpringManager.getBean(DeptRepository.class).findOne(strategy.getString("DEPT_ID"));
                saleTask.setDept(dept);
                saleTask.setDeptName(dept.getName());
                saleTask.setTaskRulesNumber(strategy.getString("ID"));
                if (Objects.equals("B", strategy.getString("CODE"))) {
                    saleTask.setSaleAmountTask(strategy.getDoubleValue("TARGET_AMOUNT"));
                } else {
                    saleTask.setCommercialInsuranceTask(strategy.getDoubleValue("TARGET_AMOUNT"));
                }
                saleTask.setEndDate(strategy.getDate("VALID_PERIOD_END"));
                saleTask.setStartDate(strategy.getDate("VALID_PERIOD_START"));
                for (int i = 0, len = saleDataArray.size(); i < len; i++) {
                    JSONObject saleData = saleDataArray.getJSONObject(i);
                    if (Objects.equals(saleData.getString("DEPT"), strategy.getString("DEPT_ID"))) {
                        saleTask.setStatisticDate(saleData.getDate("STATISTIC_DATE"));
                        saleTask.setCommercialInsuranceAmount(saleData.getDoubleValue("COMMERCIAL_INSURANCE"));
                        saleTask.setCompulsoryInsuranceAmount(saleData.getDoubleValue("COMPULSORY_INSURANCE"));
                        saleTask.setTodaySaleAmount(saleData.getDoubleValue("SALE_AMOUNT"));
                    }
                }
                saleTask = taskRepository.saveAndFlush(saleTask);
                JSONObject requestData = new JSONObject();
                requestData.put("TASK_RULES_NUMBER", saleTask.getTaskRulesNumber());
                requestData.put("DEPT_ID", saleTask.getDept().getId());
                JSONArray newDataList = saleTaskDao.queryTotalInsuranceSaleAmount(requestData);
                if (newDataList.size() != 1) {
                    throw new RuntimeException("历史数据有误!!!");
                }
                JSONObject newData = newDataList.getJSONObject(0);
                double totalSaleAmount = newData.getDoubleValue("SALE_AMOUNT");
                double totalCommercialInsurance = newData.getDoubleValue("COMMERCIAL_INSURANCE_AMOUNT");
                saleTask.setTotalSaleAmount(totalSaleAmount);
                saleTask.setTotalCommercialInsurance(totalCommercialInsurance);
                double taskProgress = 0;
                if (newData.getDoubleValue("SALE_AMOUNT_TASK") > 0) {
                    taskProgress = Arith.round(Arith.div(totalSaleAmount, saleTask.getSaleAmountTask(), 4) * 100, 2);
                } else {
                    taskProgress = Arith.round(Arith.div(totalCommercialInsurance, saleTask.getCommercialInsuranceTask(), 4) * 100, 2);
                }
                saleTask.setTaskProgress(taskProgress);
                taskRepository.saveAndFlush(saleTask);
            });
        }
    }
}
